package com.xiaoyu.lanling.feature.coin.activity

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.annotation.Keep
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.event.coin.CoinBalanceEvent
import com.xiaoyu.lanling.event.coin.CoinChargeEvent
import com.xiaoyu.lanling.feature.coin.model.CoinProductItem
import com.xiaoyu.lanling.feature.coin.viewholder.CoinProductViewHolder
import com.xiaoyu.lanling.feature.pay.data.PayData
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.router.deeplink.DeepLinkRouter
import com.xiaoyu.pay.event.PayEvent
import kotlinx.android.synthetic.main.activity_coin_product_list_and_balance.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Keep
class CoinProductListAndBalanceActivity : AppCompatToolbarActivity() {

    private val requestTag = Any()
    private val adapter = CubeRecyclerViewSimpleAdapter<CoinProductItem>()

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_coin_product_list_and_balance)
        initToolbar()

        initView()
        initAdapter()
        initBind()
        initEvent()
    }

    override fun onStartSafelyAfterAppFinishInit(isFirstTimeStart: Boolean) {
        super.onStartSafelyAfterAppFinishInit(isFirstTimeStart)
        initData()
    }

    private fun initView() {
        setTitle(R.string.coin_product_list_and_balance_title)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_coin_product_list, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.record -> {
                val domain = UserData.getInstance().tokenInfo.cookieDomains.optString("lanling")
                Router.instance.goToWebView(this, "https://$domain/coinList.html")
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun initBind() {
        activity.setOnClickListener {
            val activity = it.getViewTag<CoinChargeEvent.ChargeActivity>()
                    ?: return@setOnClickListener
            DeepLinkRouter.getInstance().dispatch(this, activity.url)
        }
    }

    private fun initAdapter() {
        adapter.setViewHolderClass(0, null, CoinProductViewHolder::class.java, 9)
        recyclerview.adapter = adapter
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: CoinChargeEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                showData(event)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: PayEvent) {
                PayData.myCoin(requestTag)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: CoinBalanceEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }
                balance.text = event.coinBalance
            }

        })
    }

    private fun initData() {
        PayData.payCoinProductListAndBalance(requestTag)
    }

    private fun showData(event: CoinChargeEvent) {
        activity.visibility = if (event.activity.isValid()) View.VISIBLE else View.GONE
        ImageViewUtil.loadImage(activity, ImageLoadParam.newBuilder().setUrl(event.activity.icon).setWidthInDP(332).setHeightInDP(80).build())
        balance.text = event.coinBalance
        adapter.list = event.productList
        adapter.notifyDataSetChanged()
        activity.setViewTag(event.activity)
    }
}